package com.wlient.springboot_quartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlient.springboot_quartz.entity.SysJob;
import com.wlient.springboot_quartz.entity.SysJobDTO;
import com.wlient.springboot_quartz.scheduler.CronTaskRegistrar;
import com.wlient.springboot_quartz.scheduler.SchedulingRunnable;
import com.wlient.springboot_quartz.service.SysJobService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (SysJob)表控制层
 *
 * @author makejava
 * @since 2021-07-29 15:23:26
 */
@RestController
@RequestMapping("sysJob")
public class SysJobController {
    private static final String success = "操作成功";
    private static final String error = "操作有误";

    /**
     * 服务对象
     */
    @Resource
    private SysJobService sysJobService;

    @Resource
    private CronTaskRegistrar cronTaskRegistrar;

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public SysJob selectOne() {
        QueryWrapper<SysJob> wrapper = new QueryWrapper<>();
        wrapper.eq("job_id", 1);
        return this.sysJobService.getOne(wrapper);
    }
    @GetMapping("/selectList")
    public List<SysJob> selectList() {
        QueryWrapper<SysJob> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_flag", 0);
        return this.sysJobService.list(wrapper);
    }
    @GetMapping("/addTaskWithParam")
    public String addTaskWithParam(String name){
        SchedulingRunnable task = new SchedulingRunnable("demoTask","taskWithParams",name);
        cronTaskRegistrar.addCronTask(task, "10,30,50 * * * * ?");
        SysJob sysJob = new SysJob();

        return success;
    }
    @GetMapping("/addTaskNoParam")
    public String addTaskNoParam(){
        SchedulingRunnable task = new SchedulingRunnable("demoTask","taskNoParams","12");
        cronTaskRegistrar.addCronTask(task, "15,35,55 * * * * ?");
        return success;
    }
    @GetMapping("/addTest")
    public String addUserBySchedule(){
        SchedulingRunnable task = new SchedulingRunnable("demoTask","taskNoParams","12");
        cronTaskRegistrar.addCronTask(task, "15,35,55 * * * * ?");
        return success;
    }
    @PostMapping("/add")
    public String addSchedule(@RequestBody SysJobDTO sysJobDTO){
        SysJob sysJob = new SysJob();
        BeanUtils.copyProperties(sysJobDTO, sysJob);
        sysJob.setDeleteFlag(0).setJobStatus("0");
        Boolean result =  sysJobService.save(sysJob);
        if (result == true){
            return success;
        }else {
            return error;
        }
    }

    @PostMapping("/update")
    public String updateSchedule(@RequestBody SysJobDTO sysJobDTO){
        SysJob sysJob = new SysJob();
        BeanUtils.copyProperties(sysJobDTO, sysJob);
        Boolean result =  sysJobService.updateById(sysJob);
        if (result == true){
            return success;
        }else {
            return error;
        }
    }

    @PostMapping("/delete")
    public String deleteSchedule(@RequestBody Integer id){
        Boolean result =  sysJobService.updateById(new SysJob().setDeleteFlag(1).setJobId(id));
        if (result == true){
            return success;
        }else {
            return error;
        }
    }



    @GetMapping("/test")
    public void test() {
        SchedulingRunnable task = new SchedulingRunnable("demoTask","taskWithParams","你好");
        cronTaskRegistrar.addCronTask(task, "15,30,45 * * * * ?");
    }

}
